<template>
  <div class="container">
    <!-- 搜索栏 -->
    <SearchBox :data="searchData" @search="handleSearch" />
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
          <el-button
          type="primary"
          plain
          size="mini"
          icon="el-icon-download"
          @click="handleExport"
          >导出</el-button>
      </el-col>
    </el-row>
    <!-- 列表 -->
    <TableBox :loading="loading" :list="list" @delete="handleDelete" @refresh="getList" />
    <!-- 分页 -->
    <pagination v-show="paginationData.total > 0" :total="paginationData.total" :page.sync="paginationData.page" :limit.sync="paginationData.size" @pagination="handlePaginationChange" />
  </div>
</template>

<script>
import SearchBox from './components/SearchBox.vue'
import TableBox from './components/TableBox.vue'
import { listOrder, listOrderExport } from '@/api/order'
import { exportFile } from '@/utils/exportFile.js'

export default {
  components: {
    SearchBox,
    TableBox
  },
  data() {
    return {
      list: [],
      loading: false,
      searchData: {
        outTradeNo: null,
        name: null,
        status: 3,
        ownType: [],
        catType: [],
        timeRange: [],
        uuid: null
      },
      paginationData: {
        total: 0,
        page: 1,
        size: 10
      }
    }
  },
  computed: {
    allOwnTypeValues() {
      const ids = []
      if (this.$store.getters.ext.ownType) {
        this.$store.getters.ext.ownType.map(item => {
          ids.push(item.dictValue)
        })
      }
      return ids
    },
    allCatTypeValues() {
      const ids = []
      if (this.$store.getters.ext.catType) {
        this.$store.getters.ext.catType.map(item => {
          ids.push(item.dictValue)
        })
      }
      return ids
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 删除
    handleDelete() {
      this.paginationData.page = 1
      this.getList()
    },
    // 搜索
    handleSearch() {
      this.paginationData.page = 1
      this.getList()
    },
    // 导出
    handleExport() {
      const { 
        outTradeNo,
        name,
        status,
        ownType,
        catType,
        timeRange,
       } = this.searchData
      const params = {
        outTradeNo,
        name,
        status,
        ownType: ownType.length ? ownType.join() : this.allOwnTypeValues.join(),
        catType: catType.length ? catType.join() : this.allCatTypeValues.join(),
        orderByColumn: 'createDate',
        isAsc: 'desc'
      }
      if (this.$store.getters.roles.includes('merchant')) {
        params.merchant = this.$store.getters.name
      }
      if (timeRange && timeRange.length) {
        params.queryStartTime = timeRange[0]
        params.queryEndTime = timeRange[1]
      }
      listOrderExport(params).then(response => {
          exportFile(response, '订单列表')
        }
      )
    },
    // 获取列表数据
    async getList() {

      const { 
        outTradeNo,
        name,
        status,
        ownType,
        catType,
        timeRange,
        uuid
       } = this.searchData
      const { page, size } = this.paginationData
      const params = {
        uuid,
        outTradeNo,
        name,
        status,
        ownType: ownType.length ? ownType.join() : this.allOwnTypeValues.join(),
        catType: catType.length ? catType.join() : this.allCatTypeValues.join(),
        pageNum: page,
        pageSize: size,
        orderByColumn: 'createDate',
        isAsc: 'desc'
      }
      if (this.$store.getters.roles.includes('merchant')) {
        params.merchant = this.$store.getters.name
      }
      console.log(this.$store.getters, params)
      if (timeRange && timeRange.length) {
        params.queryStartTime = timeRange[0]
        params.queryEndTime = timeRange[1]
      }
      this.loading = true
      listOrder(params).then(response => {
          this.list = response.rows
          this.paginationData.total = response.total
          this.loading = false
        }
      )
    },
    // 分页
    handlePaginationChange(val) {
      this.paginationData.page = val.page
      this.paginationData.size = val.limit
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;
}
</style> 